//*****************************************************************************
// JFab (http://code.google.com/p/jfab)
// Copyright (c) 2011-2012 JFab.org
// Admin jfab@jeffreythompson.net
//
// See the file "LICENSE.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//*****************************************************************************

package org.jfab.swingui.editpane;

import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.jfab.core.model.CGCompilationUnit;

/**
 * Provide an edit pane for a code generator compilation unit.
 * 
 * @param <T> Subject type.
 */
public class CGCompilationUnitEditPane<T extends CGCompilationUnit> extends
        CGObjectEditPane<T>
{
    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** Package name widget. */
    protected JTextField _packageNameUI;

    /** propertyChangeSupported attribute widget. */
    protected JCheckBox _propertyChangeSupportedUI;

    /** Type parameters widget. */
    protected JTextArea _typeParametersUI;

    /**
     * Construct this object with the given parameter.
     * 
     * @param cgCompilationUnit Code generator compilation unit.
     */
    public CGCompilationUnitEditPane(final T cgCompilationUnit)
    {
        super(cgCompilationUnit);
    }

    @Override
    public void applyChanges()
    {
        super.applyChanges();

        final CGCompilationUnit cgCompilationUnit = getSubjectObject();

        cgCompilationUnit.setPropertyChangeSupported(_propertyChangeSupportedUI
                .isSelected());

        cgCompilationUnit.getTypeParameters().clear();
        cgCompilationUnit.getTypeParameters().addAll(
                getStringListAdapter().unmarshal(_typeParametersUI.getText()));

        cgCompilationUnit.setPackageName(_packageNameUI.getText());
    }

    @Override
    protected void createWidgets()
    {
        super.createWidgets();

        _packageNameUI = createTextField();
        _propertyChangeSupportedUI = createCheckBox();
        _typeParametersUI = createTextArea();
    }

    @Override
    protected void fillPanel()
    {
        super.fillPanel();

        bindAndAdd("packageName", _packageNameUI);
        bindAndAdd("typeParameters", _typeParametersUI, getStringListAdapter());
        bindAndAdd("propertyChangeSupported", _propertyChangeSupportedUI);
    }

    @Override
    protected void fillWidgets()
    {
        super.fillWidgets();

        final CGCompilationUnit cgCompilationUnit = getSubjectObject();

        _propertyChangeSupportedUI.setSelected(cgCompilationUnit
                .isPropertyChangeSupported());
        _typeParametersUI.setText(getStringListAdapter().marshal(
                cgCompilationUnit.getTypeParameters()));
        _packageNameUI.setText(cgCompilationUnit.getPackageName());
    }
}
